import random
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import util_toml

# 设置浏览器驱动路径（以 Chrome 为例）
# driver = webdriver.Edge(service=Service(r"D:\Program Files\edgedriver_win64\msedgedriver.exe"))
driver = webdriver.Edge()
util_toml.init()
if __name__ == '__main__':
    num = 0
    query_wait = util_toml.get("query_wait")
    # 打开网页
    driver.get(util_toml.get("login_url"))
    time.sleep(2)
    while True:
        try:
            if driver.current_url.__contains__(util_toml.get("main_name")) is False:
                print("正在登录...")
                username = driver.find_element(By.ID, "txt_UserID")
                username.send_keys(util_toml.get("username"))
                psw = driver.find_element(By.ID, "txt_PptextNo")
                psw.send_keys(util_toml.get("password"))
                loginbtn = driver.find_element(By.ID, "btn-login")
                loginbtn.click()
                time.sleep(1)
            else:
                print("已进入主页...")
                # body = driver.find_element(By.TAG_NAME, "sys_allFuncs")
                # continue
                suo_you_gong_neng = driver.find_element(By.ID, "sys_allFuncs")
                suo_you_gong_neng.click()
                time.sleep(1)
                cai_wu_gong_xiang = driver.find_element(By.ID, '_easyui_tree_33')
                actions = ActionChains(driver)
                actions.double_click(cai_wu_gong_xiang).perform()
                time.sleep(1)
                yun_ying_guan_li = driver.find_element(By.ID, '_easyui_tree_37')
                yun_ying_guan_li.click()
                time.sleep(1)
                ping_zheng_zhi_liang = driver.find_element(By.ID, "FSPZJLJCC02")
                ping_zheng_zhi_liang.click()
                time.sleep(1)
                iframe = driver.find_element(By.TAG_NAME, "iframe")  # 可以通过标签名、ID 或 XPath 来定位
                # 切换回iframe
                driver.switch_to.frame(iframe)
                chou_yang = driver.find_element(By.ID, '204541b4-78c1-4bed-8312-97766ef41e0e')
                chou_yang.click()
                time.sleep(1)
                # 完成操作后，切换回主页面
                driver.switch_to.default_content()

                # 关闭打开界面
                # closes= driver.find_elements(By.CLASS_NAME,"tabs-close")
                # closes[1].click()

                iframe = driver.find_elements(By.TAG_NAME, "iframe")  # 可以通过标签名、ID 或 XPath 来定位
                driver.switch_to.frame(iframe[1])
                time.sleep(1)
                is_check = False
                while is_check is False:
                    # 点击本月
                    ben_yue = driver.find_elements(By.XPATH, '//label[@for="XRadioGroupSJFWv_1"]')
                    ben_yue[0].click()
                    # 点击了查询
                    cha_xun = driver.find_element(By.ID, "0f92870b-c7bf-4b5a-8b61-001f06e3e53c")
                    cha_xun.click()
                    time.sleep(float(query_wait))  # 等待页面加载完成
                    checkboxes = driver.find_elements(By.XPATH, '//input[@type="checkbox"]')
                    print("累积已抢单据数:" + str(util_toml.get("number")) + " | " + "本次已抢单据数:" + str(num))
                    if len(checkboxes) > 1:
                        print("有新单据了...")
                        # time.sleep(1000)

                        checkboxes[0].click()
                        # 点击确定
                        que_ren = driver.find_element(By.ID, "da2973d8-81c0-4828-9c2c-fc02228e779d")
                        que_ren.click()
                        util_toml.update("number", util_toml.get("number") + 1)
                        num = num + 1
                        # 有单据
                        is_check = True
                    else:
                        random_number = random.uniform(0.3, 2.0)
                        time.sleep(random_number)
                else:
                    # 完成操作后，切换回主页面
                    driver.switch_to.default_content()
                    # driver.refresh()
        except Exception as e:
            print(e)
            # input("程序异常暂停，按钮回车继续")
            driver.switch_to.default_content()
        print("登录失败，1秒后尝试重新登录...")
        time.sleep(1)

driver.quit()
driver.close()
